package com.ddd.demo.lee.test;

import com.ddd.demo.domain.dto.ListNode;
import com.ddd.demo.domain.dto.Node;

public class Solution2 {

    public static void main(String[] args) {

        Node listNode1 = ListNode.getListNode1(); // 8 2 3
        Node listNode2 = ListNode.getListNode2(); // 3 7 6

        Node node = addTwoNumbers(listNode1, listNode2);
        ListNode.traver(node);
    }

    public static Node addTwoNumbers(Node l1, Node l2) {

        Node dummy = new Node(-1);
        Node tmp = dummy;
        int k = 0;
        while (l1 != null || l2 != null){
            int a = l1 != null ? l1.getValue() : 0;
            int b = l2 != null ? l2.getValue() : 0;
            int sum = a + b + k;
            Node n = new Node(sum % 10);
            if(sum >= 10){
                k = 1;
            }else{
                k = 0;
            }
            tmp.setNext(n);
            tmp = tmp.getNext();
            if(null != l1){
                l1 = l1.getNext();
            }
            if(null != l2){
                l2 = l2.getNext();
            }
        }

        if(k > 0){
            tmp.setNext(new Node(k));
        }
        return dummy.getNext();
    }
}
